<?php
class checkos {
	public $OS = 'Unknow';
	public function __construct () {
		if (isset ($_SERVER) && array_key_exists ('HTTP_USER_AGENT', $_SERVER)) {
			if (preg_match ('/Android/', $_SERVER['HTTP_USER_AGENT']) === 1) {
				$this->OS = 'Android';
			} else if (preg_match ('/Adr/', $_SERVER['HTTP_USER_AGENT']) === 1) {
				$this->OS = 'Android';
			} else if (preg_match ('/iPhone/', $_SERVER['HTTP_USER_AGENT']) === 1) {
				$this->OS = 'iPhone';
			} else if (preg_match ('/iPad/', $_SERVER['HTTP_USER_AGENT']) === 1) { 
				$this->OS = 'iPhone';
			}
		}
	}
	public function getOSName () {
		return $this->OS;
	}
}

class checkcountry {
	public $Country = 'Unknow';
	public function __construct () {
		if (function_exists ('geoip_country_code_by_name') && isset ($_SERVER) && array_key_exists ('REMOTE_ADDR', $_SERVER)) {
			$iptocountry = geoip_country_code_by_name ($_SERVER['REMOTE_ADDR']);
			if ($iptocountry) {
				$this->Country = $iptocountry;
			}
		}
	}
	public function getCountryName () {
		return $this->Country;
	}
}

class download {
	public $Download = Array (
		'Android' => Array (
			'GLOBAL' => 'Lodgewood_Mongkok.apk'
		),
		'iPhone' => Array (
			'GLOBAL' => 'https://itunes.apple.com/app/xiang-gang-wang-jiao-hui-xian/id816068702'
		),
	);
	public function getdownloadpath ($OS, $Country) {
		$path = null;
		if (array_key_exists ($OS, $this->Download) && array_key_exists ($Country, $this->Download[$OS])) {
			$path = $this->Download[$OS][$Country];
		} else {
			if (array_key_exists ($OS, $this->Download) && array_key_exists ('GLOBAL', $this->Download[$OS])) {
				$path = $this->Download[$OS]['GLOBAL'];
			}
		}
		return $path;
	}
}


class mymodule {
	public function execute () {
		$download = new download ();
		$osinfo = new checkos ();
		$countryinfo  = new checkcountry ();
		$osname = $osinfo->getOSName ();
		$countryname = $countryinfo->getCountryName ();
		$path = $download->getdownloadpath ($osname, $countryname);
		$ua = '';
		$ip = '';
		if (isset ($_SERVER)) {
			if (array_key_exists ('HTTP_USER_AGENT', $_SERVER)) {
				$ua = $_SERVER['HTTP_USER_AGENT'];
			}
			if (array_key_exists ('REMOTE_ADDR', $_SERVER)) {
				$ip = $_SERVER['REMOTE_ADDR'];
			}
		}
		if ($path == null) {
			printf ("Coming soon");
		} else {
			header (sprintf ('Location: %s', $path));
		}
	}
}

$mymodule = new mymodule ();
$mymodule->execute ();
?>
